package com.fastloan.jn.mgt.product.service.impl;

import com.fastloan.jn.common.BasicServiceImpl;
import com.fastloan.jn.mgt.product.enums.JnProductEnum;
import com.fastloan.jn.mgt.product.model.JnProduct;
import com.fastloan.jn.mgt.product.model.JnProductApprove;
import com.fastloan.jn.mgt.product.service.JnProductApproveService;
import com.fastloan.jn.mgt.product.service.JnProductService;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

import static com.fastloan.jn.mgt.product.enums.JnProductEnum.*;
import static java.util.stream.Collectors.toList;

/**
 * @author chenkai
 *         createTime 2017-10-2017/10/20 14:26
 */
@Service
public class JnProductServiceImpl extends BasicServiceImpl<JnProduct> implements JnProductService {
    @Autowired
    private JnProductApproveService productApproveService;

    @Override
    public ResultTo approve(String productId) {
        JnProductApprove approve = new JnProductApprove();
        approve.setProductId(productId);
        List<JnProductApprove> approveList = productApproveService.select(approve);
        long getFinalNode = approveList.stream()
                .filter(jnProductApprove -> jnProductApprove.getOrderBy() != null)
                .count();
        if (getFinalNode <= 0) {
            throw new RuntimeException("尚未配置审批流程");
        }
        JnProduct jnProduct = selectByPrimaryKey(productId);
        if (Objects.equals(jnProduct.getProductStatus(), UNPUBLISHED.getType()))

        {
            return ReleaseToWithdraw(jnProduct, PUBLISHED);
        } else if (Objects.equals(jnProduct.getProductStatus(), PUBLISHED.getType()))

        {
            return ReleaseToWithdraw(jnProduct, UNPUBLISHED);
        }
        return new

                ResultTo(ResultEnum.OPERATION_FAILED).

                setData("无需重复发布");

    }

    /**
     * 发布/撤回产品
     *
     * @param jnProduct
     * @param jnProductEnum
     * @return
     */
    private ResultTo ReleaseToWithdraw(JnProduct jnProduct, JnProductEnum jnProductEnum) {
        jnProduct.setProductStatus(jnProductEnum.getType());
        updateByPrimaryKeySelective(jnProduct);
        return updateByPrimaryKeySelective(jnProduct) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultTo copyProduct(JnProduct product) {
        if (!product.getProductStatus().equals(PUBLISHED.getType())) {
            throw new RuntimeException("只能编辑已发布的产品");
        }
        product.setDated(PUBLISHED.getType());
        updateByPrimaryKeySelective(product);

        JnProductApprove productApprove = new JnProductApprove();
        productApprove.setProductId(product.getProductId());
        List<JnProductApprove> productApproveList = productApproveService.select(productApprove);

        product.setDated(UNPUBLISHED.getType());
        product.setProductId(null);
        insertSelective(product);

        List<JnProductApprove> approveList = productApproveList.parallelStream()
                .map(jnProductApprove -> {
                    jnProductApprove.setProductId(product.getProductId());
                    jnProductApprove.setApproveId(null);
                    return jnProductApprove;
                }).collect(toList());
        return productApproveService.insertList(approveList) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultTo updateProduct(JnProduct product) {
        if (product.getProductStatus().equals(PUBLISHED.getType())) {
            return copyProduct(product);
        }
        return updateByPrimaryKeySelective(product) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultTo deleteProduct(String productId) {
        JnProduct product = selectByPrimaryKey(productId);
        if (!product.getProductStatus().equals(UNPUBLISHED.getType())) {
            throw new RuntimeException("只能删除未发布的产品");
        }
        product.setDated(PUBLISHED.getType());
        updateByPrimaryKeySelective(product);

        JnProductApprove productApprove = new JnProductApprove();
        productApprove.setDated(PUBLISHED.getType());
        Example example = new Example(JnProductApprove.class);
        example.createCriteria().andEqualTo("productId", product.getProductId())
                .andEqualTo("orgId", ManagerKit.getOrgId());
        return productApproveService.updateByExampleSelective(productApprove, example) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);

    }
}
